﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;

namespace SCM.Manage.Controllers
{
    [Authorize]
    public class SysFunctionController : BaseController
    {
        public IActionResult Index()
        {
            BindFunction();
            return View();
        }

        #region 添加
        public ActionResult AddView()
        {
            BindFunction();
            return View();
        }

        public IActionResult Add(T_Function entity)
        {
            #region 数据验证
            if (string.IsNullOrWhiteSpace(entity.FunctionKey))
            {
                return Error("键值不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.FunctionName))
            {
                return Error("名称不能为空");
            }
            #endregion

            entity.ID = Guid.NewGuid().ToString();
            entity.CompanyID = CurrentCompanyID;
            entity.Creator = CurrentUserId;
            entity.CreatorName = CurrentUserName;

            var service = GetService<IFunctionService>();
            bool result = service.Add(entity) != null ? true : false;
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 查询
        [HttpPost]
        public IActionResult Search(string functionKey, string functionName,string parentId, int index = 1)
        {
            var service = GetService<IFunctionService>();
            var userPage = service.Query(functionKey, functionName, parentId,CurrentCompanyID, new Common.PageInfo() { PageIndex = index, PageSize = 10 });
            return GetOk(new { totalCount = userPage.Page.DataCount, items = userPage.Data });
        }

        #endregion

        #region 编辑
        public ActionResult EditView(string id)
        {
            BindFunction();
            var model = GetService<IFunctionService>().GetById(id);
            return View(model);
        }

        public IActionResult Modify(T_Function entity)
        {
            entity.ModifyUserId = CurrentUserId;
            entity.ModifyUserName = CurrentUserName;

            bool result = GetService<IFunctionService>().Update(entity);
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除  【支持批量操作】
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Delete(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids) || ids.Split(',').Length < 0)
            {
                return Error("请选择数据");
            }
            var delIds = ids.Split(',').ToList();
            bool result = GetService<IUserService>().Delete(delIds);
            return result ? GetOk(result) : Error("删除失败");
        }
        #endregion

        #region 数据绑定
        private void BindFunction()
        {
            List<SelectListItem> functionList = new List<SelectListItem>();
            functionList.Add(new SelectListItem() { Text = "请选择", Value = "", Selected = true });

            var functionService = GetService<IFunctionService>();
            var functions = functionService.GetList(CurrentCompanyID, "");
            functions.ForEach(item =>
            {
                functionList.Add(new SelectListItem() { Text = item.FunctionName, Value = item.ID });
            });

            ViewBag.FunctionList = functionList;
        }
        #endregion
    }
}
